//
//  MCTabBar.swift
//  HXWeibo
//
//  Created by HX on 15/11/23.
//  Copyright © 2015年 HX. All rights reserved.
//

import UIKit

class MCTabBar: UITabBar
{
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        let w = bounds.size.width / 5
        
        let frame = CGRect(x: 0, y: 0, width: w, height: bounds.size.height)
        
        //: 记录是第几个view
        var index = 0
        
        //: 重新设置按钮的位置
        for view in self.subviews
        {
            if view is UIControl && !(view is UIButton)
            {
                //: 撰写按钮不需要设置frame,只有系统的UITabBarButton需要设置frame
                view.frame = CGRectOffset(frame, CGFloat(index) * w, 0)
                
                index += index == 1 ? 2 : 1
            }
        }
        //: 设置撰写按钮的frame
        composeBtn.frame = CGRectOffset(frame, 2 * w, 0)
    }
    //: 懒加载撰写按钮
    lazy var composeBtn: UIButton =
    {
        let btn = UIButton()
        
        //: 设置按钮背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //: 设置按钮图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        self.addSubview(btn)
        
        return btn
    }()
}

































